package test

import com.gitee.ixtf.guice.Jguice
import com.gitee.ixtf.vertx.guice.VertxGuice
import io.vertx.core.DeploymentOptions
import io.vertx.core.Launcher
import io.vertx.core.Vertx
import io.vertx.core.VertxOptions
import io.vertx.ext.web.common.WebEnvironment.SYSTEM_PROPERTY_NAME
import io.vertx.kotlin.coroutines.coAwait
import kotlin.system.exitProcess
import kotlin.time.Duration.Companion.days
import kotlinx.coroutines.runBlocking

fun main(args: Array<String>) {
  System.setProperty(SYSTEM_PROPERTY_NAME, "dev")
  System.setProperty("ixtf.api.port", "30000")
  TestLucene().dispatch(args)
}

class TestLucene : Launcher() {
  // override fun getMainVerticle(): String = TestVerticle::class.java.name

  override fun beforeStartingVertx(options: VertxOptions) {
    super.beforeStartingVertx(options)
    with(options) { maxEventLoopExecuteTime = 1.days.inWholeNanoseconds }
  }

  override fun afterStartingVertx(vertx: Vertx) {
    super.afterStartingVertx(vertx)
    Runtime.getRuntime().addShutdownHook(Thread { runBlocking { vertx.close().coAwait() } })
    Jguice.init(TestVertxModule(vertx))
  }

  override fun handleDeployFailed(
      vertx: Vertx,
      mainVerticle: String?,
      deploymentOptions: DeploymentOptions?,
      cause: Throwable?
  ) {
    cause?.printStackTrace()
    exitProcess(1)
  }
}

class TestVertxModule(vertx: Vertx) : VertxGuice(vertx) {
  override fun configure() {
    super.configure()
  }
}
